import 'package:aqua_plunge/dao/login_dao.dart';
import 'package:aqua_plunge/http/app_manager.dart';
import 'package:aqua_plunge/services/database_service.dart';
import 'package:aqua_plunge/util/view_util.dart';
import 'package:clipboard/clipboard.dart';
import 'package:flutter/material.dart';
import 'package:flutter_util_code/toast_utils.dart';
import 'package:get/get.dart';

import '../constants/constants.dart';
import '../cover/LogPvManager.dart';
import '../page/login_page.dart';
import '../util/HiCache.dart';
import '../widget/app_bar.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final _version = '1.0.0';
  var _isSelect = false;

  get _contentView => Container(
        padding: const EdgeInsets.fromLTRB(24, 28, 24, 28),
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('images/img_primary_bg.png'),
                fit: BoxFit.cover)),
        child: ListView(
          children: [
            Container(
                alignment: Alignment.center,
                child: Image.asset(
                  "images/icon_app_logo.png",
                  width: 80,
                  height: 80,
                )),
            _buildItem(Row(
              children: [
                _buildText('ID: ${AppManager.getUserInfo().userId}'),
                Expanded(child: Container()),
                GestureDetector(
                    onTap: () {
                      FlutterClipboard.copy(
                              AppManager.getUserInfo().userId ?? "")
                          .then((value) {
                        ToastUtils.showCenter("success");
                      });
                    },
                    child: Image.asset(
                      "images/icon_copy.png",
                      width: 19,
                      height: 19,
                    ))
              ],
            )),
            _buildItem(Row(
              children: [
                _buildText("Version"),
                Expanded(child: Container()),
                _buildText(_version)
              ],
            )),
            _buildItem(Row(
              children: [
                _buildText("Auto Translate"),
                Expanded(child: Container()),
                Switch(
                    value: _isSelect,
                    onChanged: (value) {
                      setState(() {
                        _isSelect = value;
                      });
                    })
              ],
            )),
            _buildItem(InkWell(
              child: Container(
                alignment: Alignment.centerLeft,
                child: _buildText("Terms and Conditions"),
              ),
              onTap: () {
                jumpH5(
                    url: Constants.termConditions,
                    title: 'Terms and Conditions');
              },
            )),
            _buildItem(InkWell(
              onTap: () {
                jumpH5(url: Constants.privacy, title: 'Privacy Policy');
              },
              child: Container(
                alignment: Alignment.centerLeft,
                child: _buildText("Privacy Policy"),
              ),
            )),
            _buildItem(InkWell(
              onTap: () {
                LogPvManager.stat(page: Constants.pageLogout);
                LoginDao.logout();
              },
              child: Row(
                children: [
                  _buildText("Logout"),
                  Expanded(child: Container()),
                  Image.asset("images/icon_arrow_next.png",
                      width: 20, height: 20)
                ],
              ),
            )),
            _buildItem(InkWell(
              onTap: () {
                showDialog(context: context, builder: (_){
                   return AlertDialog(
                     title: const Text('Are you sure want to delete the account?'),
                     actions: [
                       TextButton(onPressed: (){
                         Navigator.of(context).pop();
                       }, child: const Text('Return')),
                       TextButton(onPressed: (){
                         LoginDao.deleteAccount((){
                           _deleteAccount();
                         });
                       }, child:const Text('Yes'))
                     ],
                   );
                });
              },
              child: Row(
                children: [
                  _buildText("Delete Account"),
                  Expanded(child: Container()),
                  Image.asset("images/icon_arrow_next.png",
                      width: 20, height: 20)
                ],
              ),
            )),
          ],
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: appBar(""), body: _contentView);
  }

  Widget _buildText(String string) {
    return Text(string,
        style: const TextStyle(color: Colors.white, fontSize: 14));
  }

  Widget _buildItem(Widget childview) {
    return Container(
      height: 60,
      margin: const EdgeInsets.only(top: 20),
      width: double.infinity,
      padding: const EdgeInsets.only(left: 20, right: 20),
      decoration: BoxDecoration(
        image: const DecorationImage(
            image: AssetImage('images/list_item_bg.png'), fit: BoxFit.cover),
        borderRadius: BorderRadius.circular(12),
      ),
      child: childview,
    );
  }

  void _deleteAccount() {
    AppManager.resetUserinfo();
    DatabaseService.instance.clearAllTable();
    HiCache.getInstance().clearSharedPreferences();
    Navigator.of(Get.context!).pop();
    Get.offAll(const LoginPage());
    LogPvManager.stat(page: Constants.pageDeleteAccount);
  }
}
